
package com.ce.pms.custservice.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.util.Assert;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.charge.service.INonConventionalChargeService;
import com.ce.pms.charge.service.IParameterDefService;
import com.ce.pms.charge.vo.ChargeRecordVO;
import com.ce.pms.charge.vo.ParameterDefVO;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.custservice.dao.IBillDao;
import com.ce.pms.custservice.dao.IDispatchFormDao;
import com.ce.pms.custservice.model.BillDO;
import com.ce.pms.custservice.model.DispatchFormDO;
import com.ce.pms.custservice.service.ICustServChargeService;
import com.ce.pms.custservice.vo.BillVO;
import com.ce.pms.exception.BusinessException;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
/**
 * 
 * 	名称: com.ce.pms.custservice.service.impl.CustServChargeServiceImpl <br>
 *  服务收费业务处理
 *
 * @author  付湘辉  <br>
 * Created on： 2008-12-22 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class CustServChargeServiceImpl extends PmsBaseServiceImpl implements ICustServChargeService {
	private static final String CUSTOMER_EVENT_CHARGE_ITEM_NAME = "客服事件派工收费";
	private IBillDao billDao;
	private IDispatchFormDao dispatchFormDao;
    private INonConventionalChargeService nonConventionalChargeService;
    private IParameterDefService parameterDefService;

   /**
    * 功能描述:  新增帐单 <br>
	* @param dFormID - 派工单ID
	* @param billVO - 帐单VO
    * @roseuid 49436CAE037A
    * 
    * Create author:付湘辉 <br>
    * Create on:2008-12-15  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public BillVO addBill(Long dFormID, BillVO billVO) {
	   Assert.notNull(billVO);
	   DispatchFormDO dispatchFormDo =  this.dispatchFormDao.findById(dFormID);
	   Boolean isAllow = dispatchFormDo.isAllowAddBill();
	   if (!isAllow) {
		   throw new BusinessException("custservice:custservice_cannot_addbillfordform_exception",
				   				BusinessException.WARNING);
	   }

	   BillDO billDo = new BillDO();
	   this.assembleV2D(billVO, billDo);
	   SysInfo sysInfo = super.createInfo(billVO.getCreator());
	   billDo.setSysInfo(sysInfo);
	   this.billDao.saveObject(billDo);
	   billVO.setSysDate(sysInfo.getSysDate());
	   billVO.setKeyword(billDo.getKeyword());
	   return billVO;
   }
   
   /**
    * 功能描述:检查指定的账单是否允许修改  <br>
    * @param billId 账单id
    *
    * Create author:付湘辉 <br>
    * Create on:2009-1-9  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public void checkIsAllowEditBill(Long billId){
	   BillDO billDO = this.billDao.findById(billId);
	   //检查
	   if (!billDO.isAllowUpdate()) {
		   throw new BusinessException("custservice:servcharge_bill_state_not_new_exception",
				   												BusinessException.WARNING);
	   }
   }
   
   /**
    * 功能描述:传递费用  <br>
    * @param billID - 帐单ID
    * @param operator
    * @roseuid 49436CAE03D8
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-26  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public BillVO delivery(Long billID, Long operator) {
	   BillDO billDo = this.billDao.findById(billID);
	   //已传递则不能再次传递
	   if (BillDO.BILL_STATUS_TRANSLATE_CODE.equals(billDo.getStatuz())) {
		   throw new BusinessException("custservice:servcharge_cannot_delivery_istranslate_exception"
				   				,BusinessException.WARNING)	;
	   }
	   //修改系统信息
	   super.modifyInfo(billDo.getSysInfo(), operator);

	   ChargeRecordVO chargeRecordVO = new ChargeRecordVO();
	   //组装费用记录VO
	   this.assembleBillDO2ChrgRecordVO(billDo, chargeRecordVO);
	   
	   this.checkHasHouse(chargeRecordVO);
	   
	   //增加费用记录
	   chargeRecordVO = this.nonConventionalChargeService.addChargeRecord(chargeRecordVO);
	   //写入费用记录ID到账单中
	   billDo.writeBackTransactionID(chargeRecordVO.getKeyword());

	   //设置账单状态
	   billDo.setStatuzHasDelivery();

	   //更新账单
	   this.billDao.updateObject(billDo);
	   BillVO billVO = new BillVO();
	   this.assembleD2V(billDo, billVO);
	   return billVO;
   }
   
   /**
    * 功能描述: 修改帐单 <br>
    * @param billVO - 帐单VO
    * @roseuid 49436CAE03C8
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-15  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public BillVO editBill(BillVO billVO){
	   Assert.notNull(billVO);
	   BillDO billDO = this.billDao.findById(billVO.getKeyword());
	   //检查
	   this.checkIsAllowEditBill(billVO.getKeyword());
	   //装配DO
	   this.assembleV2D(billVO, billDO);
	   //修改系统信息
	   super.modifyInfo(billDO.getSysInfo(), billVO.getModifier());
	   BeanUtil.convertVoAsDo(billDO.getSysInfo(), billVO);
	   this.billDao.updateObject(billDO);
	   return billVO;
   }
   
   /**
    * 功能描述: 根据ID查询帐单 <br>
    * @param id
    * @return BillVO
    * @roseuid 49436CAF003E
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-23  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public BillVO getBillByID(Long id) {
	   Assert.notNull(id);
	   BillDO billDO = this.billDao.findById(id);
	   BillVO billVO = new BillVO();
	   this.assembleD2V(billDO, billVO);

	   return billVO;
   }
   /**
    * 功能描述:查询指定派工单的账单  <br>
    * @param dispatchFormId 派工单ID
    * @return
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-30  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public BillVO getBillVOByDispatchFormId(Long dispatchFormId){
	   Assert.notNull(dispatchFormId);
	   BillDO billDO = this.billDao.getBillDOByDispatchFormId(dispatchFormId);
	   BillVO billVO = new BillVO();
	   if (!BlankUtil.isBlank(billDO)) {
		   this.assembleD2V(billDO, billVO);
	   }
	   
	   return billVO;
   }
   
   /**
    * 功能描述：通知更新帐单应收金额，只有新建状态的账单才能更新。
    * @param dFormID - 派工单ID
    * @param sumAmt - 应收金额
    * @roseuid 49436CAF0001
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-17  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public void noteUpdateBill(Long dFormID, BigDecimal sumAmt) {
	   BillDO bill = this.billDao.getBillDOByDispatchFormId(dFormID);
	   
	   if (BlankUtil.isBlank(bill)) {
		   return;
	   }
		// 只有新建状态的账单才能更新
	   this.checkIsAllowEditBill(bill.getKeyword());

	   bill.updateReceivable(sumAmt);
	   this.billDao.updateObject(bill);
   }
   
/**
    * @param searchVO
    * @return Page
    * @roseuid 49436CAF008C
    */
   @Deprecated
   public Page queryBillByPage(SearchVO searchVO) 
   {
    return null;
   }
   /**
    * @param searchVO
    * @return List<BillVO>
    * @roseuid 49436CAF004E
    */
   @Deprecated
   public List<BillVO> queryBillList(SearchVO searchVO) 
   {
    return null;
   }

   public void setBillDao(IBillDao billDao) {
		this.billDao = billDao;
	}

   /**
 * @param dispatchFormDao the dispatchFormDao to set
 */
public void setDispatchFormDao(IDispatchFormDao dispatchFormDao) {
	this.dispatchFormDao = dispatchFormDao;
}
   
   /**
 * @param nonConventionalChargeService the nonConventionalChargeService to set
 */
public void setNonConventionalChargeService(
		INonConventionalChargeService nonConventionalChargeService) {
	this.nonConventionalChargeService = nonConventionalChargeService;
}
   
   public void setParameterDefService(IParameterDefService parameterDefService) {
	this.parameterDefService = parameterDefService;
}

   /**
    * 功能描述:根据BillDO组装一个ChargeRecordVO  <br>
    * @param billDo
    * @param chargeRecordVO
    * @return
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-26  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   private ChargeRecordVO assembleBillDO2ChrgRecordVO(BillDO billDo,ChargeRecordVO chargeRecordVO)	{
	   if (!BlankUtil.isBlank(billDo.getTheDispatchFormDO())
			   && !BlankUtil.isBlank(billDo.getTheDispatchFormDO().getHouse())) {
		   chargeRecordVO.setHouseId(billDo.getTheDispatchFormDO().getHouse().getKeyword());
	   }
	   
	   chargeRecordVO.setBizModule(SysrefcodeDO.FEE_CHARGE_RECORD_BIZ_MOUDLE_SER);
	   chargeRecordVO.setOperationType(SysrefcodeDO.FEE_CHARGE_RECORD_OPERATION_TYPE_PROPERTY);
	   chargeRecordVO.setBizType(SysrefcodeDO.FEE_CHARGE_RECORD_BIZ_TYPE_CSDISPATCH);
	   chargeRecordVO.setBizId(billDo.getKeyword());
	   chargeRecordVO.setCreator(billDo.getSysInfo().getModifier());
	   chargeRecordVO.setEnterAccountMethod(SysrefcodeDO.FEE_CHARGE_RECORD_ENTER_ACCOUNT_METHOD_MERGE);
	   chargeRecordVO.setHandleYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
	   chargeRecordVO.setSum(billDo.getCredit());
		//	 交易码id
	   chargeRecordVO.setBusinessId(this.getCustServiceBizCodeID());
	   //费用项目名称
	   if (!BlankUtil.isBlank(chargeRecordVO.getBusinessId())) {
//		   BusinessCodeVO businessVo =this.businessCodeService.findBusinessCodeByKeyword(chargeRecordVO.getBusinessId());
		   chargeRecordVO.setItemName(CUSTOMER_EVENT_CHARGE_ITEM_NAME);
	   }

	   String remark = billDo.getRemark()==null?"":billDo.getRemark();
	   SysrefcodeDO mode = this.sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.CE_CUST_SERVICE_BILL_MODE_CODE, billDo.getMode());
	   StringBuilder sb = new StringBuilder(billDo.getCredit().toString());
	   sb.append(" ").append(mode.getName())
	   	  .append(" ").append(remark);
	   chargeRecordVO.setRemark(sb.toString());
	   return chargeRecordVO;
   }
   
   /**
    * 功能描述:DO 2 VO  <br>
    * @param billDO - 帐单DO
    * @param billVO - 帐单VO
    * @roseuid 49339EEF02AF
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-23  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   private BillVO assembleD2V(BillDO billDO, BillVO billVO) {
	   BeanUtil.convertVoAsDo(billDO, billVO);
	   DispatchFormDO dispatchFormDo = billDO.getTheDispatchFormDO();
	   if (!BlankUtil.isBlank(dispatchFormDo)) {
		   billVO.setDispatchFormId(dispatchFormDo.getKeyword());
	   }

	   BeanUtil.convertVoAsDo(billDO.getSysInfo(), billVO);
	   return billVO;
   }
   
   /**
    * 功能描述:VO 2 DO  <br>
    * @param billVO - 帐单VO
    * @param billDO - 帐单DO
    * @roseuid 49339F41030D
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-23  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   private BillDO assembleV2D(BillVO billVO, BillDO billDO) {
	   BeanUtil.convertVoAsDo(billVO, billDO);
	   
	   DispatchFormDO theDispatchFormDO = this.dispatchFormDao.loadById(billVO.getDispatchFormId());
	   billDO.setTheDispatchFormDO(theDispatchFormDO);

	   return billDO;
   }

  /**
    * 功能描述:  <br>
    * @param chargeRecordVO
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-31  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   private void checkHasHouse(ChargeRecordVO chargeRecordVO){
	   if (BlankUtil.isBlank(chargeRecordVO.getHouseId()) ) {
		   throw new BusinessException("custservice:custservice_cannotdelivery_ifnothouse_exception",
				   				BusinessException.WARNING);
	   }
   }

/**
    * 功能描述:  查询客服收费交易码ID <br>
    * @return Long
    * @roseuid 49349FEE01F4
    *
    * Create author:付湘辉 <br>
    * Create on:2008-12-23  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   private Long getCustServiceBizCodeID() {
	   ParameterDefVO param = this.parameterDefService.findParameterDefByPkey(SysrefcodeDO.CE_CSUT_SERVICE_EVENT_CODE);
	   Assert.notNull(param);
	   return param.getPvalue();
   }
/**
    * 调用费用服务接口ITransactionService,抛帐方法postAnDebit.
    * @param houseNumber - 房号
    * @param bizCodeID - 客服收费交易码
    * @param debitAmt - 收费金额(帐单中的实收金额)
    * @param remark - 备注:
    * 帐单中的"收费方式/"+"应收金额/"+"实收金额/"+"备注"
    * @roseuid 493F794E002E
    */
   @SuppressWarnings("unused")
   private void postAnDebit(String houseNumber, Long bizCodeID, BigDecimal debitAmt, String remark) {
    
   }
}
